home *** CD-ROM | disk | FTP | other *** search
/ Aminet 24 / Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso / Aminet / dev / c / cpost_1_4.lha / parsearg.c < prev    next >
C/C++ Source or Header  |  1995-05-09  |  14KB  |  392 lines

  1. /*------------------------------------------------------------------*/
  2. /* parsearg : parse parameters and options in an argv list          */
  3. /*            see parsearg.h for a description                      */
  4. /*------------------------------------------------------------------*/
  5. /* 03-13-90 originally by Patrick J. Mueller                        */
  6. /* 01-09-91 version 2.0 by Patrick J. Mueller                       */
  7. /* 04-29-91 version 3.0 by Patrick J. Mueller                       */
  8. /*------------------------------------------------------------------*/
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <stdarg.h>
  13. #include <string.h>
  14. #include <ctype.h>
  15.  
  16. /*------------------------------------------------------------------*/
  17. /* typedefs                                                         */
  18. /*------------------------------------------------------------------*/
  19. typedef enum
  20.    {
  21.    Boolean_Switch,
  22.    Variable_Switch
  23.    } Item_Type;
  24.  
  25. typedef struct Cmdline_Item
  26.    {
  27.    Item_Type             type;
  28.    int                   position;
  29.    char                  sw_char;
  30.    void                 *variable;
  31.    struct Cmdline_Item  *next;
  32.    } Cmdline_Item;
  33.  
  34. /*-/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\-*/
  35. /*------------------------------------------------------------------*/
  36. /*               L O C A L   F U N C T I O N S                      */
  37. /*------------------------------------------------------------------*/
  38. /*-\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-*/
  39.  
  40. /*------------------------------------------------------------------*/
  41. /* Search the Cmdline_Item list for a particular switch.  Look for  */
  42. /* the option with a particular switch character (Boolean and       */
  43. /* Variable are treated as the same)                                */
  44. /*------------------------------------------------------------------*/
  45.  
  46. static Cmdline_Item *get_item(
  47.    Cmdline_Item *head,
  48.    char          sw_char,
  49.    int           case_sense
  50.    )
  51.  
  52.    {
  53.    Cmdline_Item *next;
  54.  
  55.    /*---------------------------------------------------------------*/
  56.    /* traverse the linked list ...                                  */
  57.    /*---------------------------------------------------------------*/
  58.    next = head;
  59.    while (next != NULL)
  60.       {
  61.       /*------------------------------------------------------------*/
  62.       /* for case sensitive switches, just compare chars            */
  63.       /*------------------------------------------------------------*/
  64.       if (case_sense)
  65.          {
  66.          if (next->sw_char == sw_char)
  67.             return(next);
  68.          }
  69.  
  70.       /*------------------------------------------------------------*/
  71.       /* otherwise, toupper the chars and compare                   */
  72.       /*------------------------------------------------------------*/
  73.       else
  74.          {
  75.          if (toupper(sw_char) == toupper(next->sw_char))
  76.             return(next);
  77.          }
  78.  
  79.       /*------------------------------------------------------------*/
  80.       /* no matches so traverse to next item                        */
  81.       /*------------------------------------------------------------*/
  82.       next = next->next;
  83.       }
  84.  
  85.    /*---------------------------------------------------------------*/
  86.    /* no matches at all!                                            */
  87.    /*---------------------------------------------------------------*/
  88.    return(NULL);
  89.    }
  90.  
  91. /*-/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\-*/
  92. /*------------------------------------------------------------------*/
  93. /*               M A I N     F U N C T I O N                        */
  94. /*------------------------------------------------------------------*/
  95. /*-\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-*/
  96.  
  97. /*------------------------------------------------------------------*/
  98. /* the main function                                                */
  99. /*------------------------------------------------------------------*/
  100. void parsearg(
  101.    int    *argc,
  102.    char  **argv,
  103.    int     case_sense,
  104.    char   *env_var,
  105.    char   *delimiters,
  106.    char   *format_string,
  107.    ...
  108.    )
  109.  
  110.    {
  111.    char         *tok;
  112.    Cmdline_Item *item_head;
  113.    Cmdline_Item *item_tail;
  114.    Cmdline_Item *item;
  115.    va_list       arg_marker;
  116.    int           i;
  117.    int           parms;
  118.    int          *ptr_int;
  119.    char        **ptr_ptr_char;
  120.    char          sw_char;
  121.    char         *env_value;
  122.    int           envc;
  123.    char         *envv;
  124.    char         *temp;
  125.  
  126.    /*---------------------------------------------------------------*/
  127.    /* sanity checks                                                 */
  128.    /*---------------------------------------------------------------*/
  129.    if ((NULL == format_string)  ||
  130.        (NULL == argv)           ||
  131.        (0    == *argc))
  132.       return;
  133.  
  134.    /*---------------------------------------------------------------*/
  135.    /* make a copy of the format string since we will be strtok()ing */
  136.    /* through it                                                    */
  137.    /*---------------------------------------------------------------*/
  138.    temp = malloc(1+strlen(format_string));
  139.    if (NULL == temp)
  140.       {
  141.       puts("Error allocating memory in parsearg()");
  142.       return;
  143.       }
  144.  
  145.    strcpy(temp,format_string);
  146.    format_string = temp;
  147.  
  148.    /*---------------------------------------------------------------*/
  149.    /* get environment variable value                                */
  150.    /*---------------------------------------------------------------*/
  151.    env_value = NULL;
  152.  
  153.    if (NULL != env_var)
  154.       if ('\0' != *env_var)
  155.          {
  156.  
  157.          /*---------------------------------------------------------*/
  158.          /* get value and copy if we found something                */
  159.          /*---------------------------------------------------------*/
  160.          env_value = getenv(env_var);
  161.  
  162.          if (NULL != env_value)
  163.             {
  164.             temp = malloc(1+strlen(env_value));
  165.             if (NULL == temp)
  166.                {
  167.                puts("Error allocating memory in parsearg()");
  168.                return;
  169.                }
  170.  
  171.             strcpy(temp,env_value);
  172.             env_value = temp;
  173.             }
  174.          }
  175.  
  176.    /*---------------------------------------------------------------*/
  177.    /* build option list                                             */
  178.    /*---------------------------------------------------------------*/
  179.    item_head = item_tail = NULL;
  180.    va_start(arg_marker,format_string);
  181.  
  182.    /*---------------------------------------------------------------*/
  183.    /* parse the format_string with strtok                           */
  184.    /*---------------------------------------------------------------*/
  185.    tok = strtok(format_string," ");
  186.  
  187.    while (NULL != tok)
  188.       {
  189.       /*------------------------------------------------------------*/
  190.       /* allocate area for a new Item                               */
  191.       /*------------------------------------------------------------*/
  192.       item = (Cmdline_Item *) malloc(sizeof(Cmdline_Item));
  193.       if (NULL == item)
  194.          {
  195.          puts("Error allocating memory in parsearg()");
  196.          return;
  197.          }
  198.  
  199.       /*------------------------------------------------------------*/
  200.       /* start assigning values to it                               */
  201.       /*------------------------------------------------------------*/
  202.       item->next    = NULL;
  203.       item->sw_char = *tok++;
  204.  
  205.       /*---------------------------------------------------------*/
  206.       /* is it a boolean switch?                                 */
  207.       /*---------------------------------------------------------*/
  208.       if ('\0' == *tok)
  209.          item->type = Boolean_Switch;
  210.  
  211.       /*---------------------------------------------------------*/
  212.       /* must be a variable switch                               */
  213.       /*---------------------------------------------------------*/
  214.       else
  215.          item->type = Variable_Switch;
  216.  
  217.       /*------------------------------------------------------------*/
  218.       /* now get the variable pointer                               */
  219.       /*------------------------------------------------------------*/
  220.       item->variabl